using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class smartfish : Creation
{
    public float Runspeed;
    public float deltadistance;
    Animator animator;
    GameObject enemy;
    public Transform player_transform;
    Transform ttransform;
    public int flip;
    private Rigidbody2D myRigidbody;
    smartfishattack smartfishattack;
    findenemy Findenemy;
    public bool randrun;
    Vector3 randposition;
    int sleeptimes=300;
    SkillCivilization civilization;
    public float dietime=60f;

    public void Start()
    {
        animator = GetComponent<Animator>();
        myRigidbody = GetComponent<Rigidbody2D>();
        ttransform = GetComponent<Transform>();
        flip = 1;
        smartfishattack = GetComponentInChildren<smartfishattack>();
        Findenemy = GetComponentInChildren<findenemy>();
        randrun = false;
        civilization=GameObject.FindWithTag("Player").GetComponentInChildren<SkillCivilization>();
        if (civilization != null && transform.parent == null)
        {
            smartfishattack.capture = civilization.capture;
            health = health * (1 + civilization.healthratio);
            smartfishattack.damage *= (1 + civilization.damageratio);
        }
    }
    //public IEnumerator Stiff()
    //{

    //myRigidbody.Sleep();

    //   yield return new WaitForSeconds(stifftime);
    // myRigidbody.WakeUp();
    //   freeze = 1;
    //   animator.SetBool("stiff", false);
    //}
    public override void TakeDamage(float damage)
    {
        health -= damage;
        if (health <= 0)
        {
            Destroy(gameObject);
        }

        //StartCoroutine(Stiff());
    }
    void Flip()
    {
        Vector3 position=randrun?randposition: player_transform.position; 
        if (flip * (position.x - ttransform.position.x) < 0)
        {

            ttransform.localRotation = Quaternion.Euler(0, 0, 0);
            smartfishattack.face = 1;

        }
        else if (flip * (position.x - ttransform.position.x) > 0)
        {
            ttransform.localRotation = Quaternion.Euler(0, 180, 0);
            smartfishattack.face = -1;
        }
    }
    void Move()
    {
        Vector3 position = randrun ? randposition : player_transform.position;
        if (position.x + deltadistance < ttransform.position.x)
        {

            animator.SetBool("move", true);
            Vector2 vel = new Vector2(-Runspeed, myRigidbody.velocity.y);
            myRigidbody.velocity = vel;
        }
        else if (position.x - deltadistance > ttransform.position.x)
        {
            animator.SetBool("move", true);
            Vector2 vel = new Vector2(Runspeed, myRigidbody.velocity.y);
            myRigidbody.velocity = vel;
        }
        else
        {
            animator.SetBool("move", false);
            Vector2 vel = new Vector2(0, myRigidbody.velocity.y);
            myRigidbody.velocity = vel;
        }
    }
    private void getplayer()
    {
        if (player_transform != null) return;
        
        if (transform.parent != null)
        {
            if (transform.parent.GetComponent<pylon>().enemy != null)
            {
                randrun = false;
                player_transform = transform.parent.GetComponent<pylon>().enemy.transform;
                return;
            }
            
        }
        enemy = Findenemy.enemy;  
        if (enemy != null)
        {
            randrun = false;
            player_transform = enemy.transform;
            return;
        }
                
        //this.transform.parent
        //player_transform = player.GetComponent<Transform>();
        //creations = GameObject.FindGameObjectsWithTag("Creation");
        //foreach (GameObject creation in creations)
        //{
        //    if (Mathf.Abs(creation.GetComponent<Transform>().position.x - transform.position.x) < Mathf.Abs(player_transform.position.x - transform.position.x))
        //    {
        //        player_transform = creation.GetComponent<Transform>();
        //    }
        //}
        if (randrun==false)
        {
            randrun = true;
            randposition= new(ttransform.position.x+Random.Range(-15.0f, 15.0f), ttransform.position.y);
            //Debug.Log(randrun);
            sleeptimes = 300;
        }
        if (randposition.x + deltadistance >= transform.position.x && randposition.x - deltadistance <= transform.position.x)
        {
            sleeptimes--;
            if(sleeptimes==0)
                randrun = false;
        }
    }
    public void Update()
    {
        dietime -= Time.deltaTime;
        if (dietime < 0)
        {
            Destroy(gameObject);
        }
        getplayer();
        if (player_transform != null||randrun)
        {
            Move();
            Flip();
        }

    }
}
